package com.nongfuspring.nongfu.controller;

import com.nongfuspring.nongfu.pojo.Opinion;
import com.nongfuspring.nongfu.pojo.ResultBean;
import com.nongfuspring.nongfu.pojo.User;
import com.nongfuspring.nongfu.service.SystemService;
import com.nongfuspring.nongfu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class SystemController {
    @Autowired
    SystemService systemService;

    @Autowired
    UserService userService;

    @Autowired
    PasswordEncoder passwordEncoder;

    @GetMapping("/getRoles")
    public HashMap<String, Object> getRoles (String userId){
        ArrayList<String> RolesList = systemService.getRoles(userId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("mesg",RolesList);
        return map;

    }

    @PostMapping("/index")
    public ModelAndView Hello1 (){
        return new ModelAndView("index");
    }


    @PostMapping("/register")
    public ResultBean register(@RequestBody User user){
        user.setPassword(passwordEncoder.encode("123456"));
        user.setAccountNonExpired(true);
        user.setEnabled(true);
        user.setAccountNonLocked(true);
        user.setCredentialsNonExpired(true);
        int i = systemService.add(user);
        Integer ii = userService.setRoleForUser(user);

        if(1 == i && ii ==1){
            return  ResultBean.ok("成功");
        }else {
            return  ResultBean.fail("失败");
        }
    }

    @GetMapping("/getUsers")
    public HashMap<String, Object> getUsers(@RequestParam(value = "pageSizes", required = false, defaultValue = "10")int pageSizes ,
                               @RequestParam(value = "pageNum", required = false, defaultValue = "1")int pageNum){
        List<User> userLists = userService.getUsers(pageSizes,pageNum);
        Integer total = userService.getTotalUsers();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("data",userLists);
        map.put("total",total);
        return map;
    }

    /**
     * create by: huad.xu
     * description: TODO
     * create time: 2022/3/10 0:11
     *
     * @param user
     * @return * @return {@link com.nongfuspring.nongfu.pojo.ResultBean }
     */
    @PostMapping("updateUser")
    public ResultBean updateUser(@RequestBody User user){
        Integer i = userService.updateUser(user);

        if(1 == i){
            return  ResultBean.ok("成功");
        }else {
            return  ResultBean.fail("失败");
        }
    }

    @PostMapping("/addOpinion")
    public ResultBean addOpinion(@RequestBody Opinion opinion){
        // 初始化
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 获取 当前系统默认时区 的 本地时间
        LocalDateTime nowDateTime = LocalDateTime.now();

        // 格式化时间
        String dateTimeString = formatter.format(nowDateTime);
        opinion.setCreateTime(dateTimeString);
        Integer i = userService.addOpinion(opinion);

        if(1 == i){
            return  ResultBean.ok("成功");
        }else {
            return  ResultBean.fail("失败");
        }
    }

    @GetMapping("/getOpinion")
    public ResultBean getOpinion(){
       ArrayList<Opinion> opinionArrayList = systemService.getOpinion();
       return ResultBean.ok(opinionArrayList);
    }

    @GetMapping("/deleteUser")
    public ResultBean deleteUser(String username){
        Integer i = systemService.deleteUser(username);
        if(1 == i){
            return  ResultBean.ok("删除成功");
        }else {
            return  ResultBean.fail("删除失败");
        }
    }







}
